import { AppDataSource } from "@/config/database";
import { Customer } from "@/entities/basis/customer.entity";
import { ErrorHandler } from "@/middlewares/error.middleware";
import { Context } from "koa";
import dayjs from "dayjs";

/** 获取客户详情 */
export async function getCustomerDetail(ctx: Context) {
    const { id } = ctx.params;

    if (!id) {
        throw new ErrorHandler('客户ID不能为空', 400);
    }

    const customerRepository = AppDataSource.getRepository(Customer);

    // 查找客户
    const customer = await customerRepository.findOne({
        where: { id: id as any }
    });

    if (!customer) {
        throw new ErrorHandler('客户不存在', 404);
    }

    // 计算年龄
    const age = dayjs().diff(customer.birthday, 'year');

    ctx.status = 200;
    ctx.body = { 
        ...customer, 
        currentPoints: Number(customer.currentPoints),
        age 
    }
} 